import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;

import javax.swing.JFileChooser;

import output.html.Combat_index;
import output.html.Combat_output;
import output.txt.Energy_output;

import objects.Combat;
import objects.Skill_entry;
import objects.Skill_information;
import objects.Entity;

import libutil.fileloader;
import libutil.fileutil;
import libutil.filewriter;
import parser.Entry;
import parser.date_parser;
import parser.parser;
import utils.Ids_actions;
import utils.ids_special_events;
import utils.utils_parser;


public class main {

	
	@SuppressWarnings("unchecked")
	public static void main(String[] args){
		JFileChooser filechoose = new JFileChooser();
		filechoose.showOpenDialog(null);
		String filepath = filechoose.getSelectedFile().getAbsolutePath();
		System.out.println(filepath);
		String dir = utils_parser.getDate(filepath);
		fileutil util = new fileutil();
		util.makedirectory(dir);
		fileloader file = new fileloader(filepath);
		parser myparser = new parser(file);
		int line = 0;
		try {
			line = myparser.parsefile();
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(line);
		System.out.println(myparser.getEntries().size());
		ArrayList<long[]> combat_times = myparser.detect_Combats(myparser.getOwner());
		for(int i=0;i<combat_times.size();i++){
			System.out.println(date_parser.get_time_formated(combat_times.get(i)[0])+" : "+date_parser.get_time_formated(combat_times.get(i)[1])+" = "+(double)((combat_times.get(i)[1]-combat_times.get(i)[0])/1000));
		}
		ArrayList<Combat> combats = new ArrayList<Combat>();
		ArrayList<Entry> tempentries = new ArrayList<Entry>();
		ArrayList<String> filenames = new ArrayList<String>();
		for(int i =0; i<combat_times.size();i++){
			ArrayList<Entry> temp = new ArrayList<Entry>();
			boolean ended = false;
			int j = 0;
			long start = combat_times.get(i)[0];
			long end = combat_times.get(i)[1];
			if((end-start)>5000){
				while (!ended){
					Entry tempentry = myparser.getEntries().get(j);
					if((long)tempentry.getTime()>start){
						if((long)tempentry.getTime()<end){
							temp.add(tempentry);
							j++;
						}else{
							ended = true;
						}
					}else{
						j++;
					}
				}
				Combat temp_combat = new Combat(temp, myparser.getOwner());
				temp_combat.setplayer_name(myparser.getMyplayer().get_name(myparser.getOwner()));
				combats.add(temp_combat);
				tempentries.addAll(temp_combat.getNoskill_entries());
				Combat_output output = new Combat_output(temp_combat, util);
				filenames.add(output.write_html_output());
			}
		}
		Combat_index index = new Combat_index(filenames, util);
		index.write_html();
	}
	
}
